package com.tjetc.controller;

import com.tjetc.constant.CommonConstant;
import com.tjetc.context.BaseContext;
import com.tjetc.dao.GuestDao;
import com.tjetc.dao.impl.GuestDaoImpl;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.BookingDTO;
import com.tjetc.pojo.dto.UserDTO;
import com.tjetc.pojo.entity.Guest;
import com.tjetc.pojo.entity.Hotel;
import com.tjetc.pojo.entity.Room;
import com.tjetc.service.*;
import com.tjetc.service.impl.*;
import com.tjetc.view.BookingCRUDPage;
import com.tjetc.view.HistoryPage;
import com.tjetc.view.HotelBrowserPage;
import com.tjetc.view.impl.BookingCRUDPageImpl;
import com.tjetc.view.impl.HistoryPageImpl;
import com.tjetc.view.impl.HotelBrowserPageImpl;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/10 19:21
 * @Description: 顾客控制器
 */
public class UserController {

    private UserService userService = new UserServiceImpl();

    private HotelService hotelService = new HotelServiceImpl();

    private HotelBrowserPage hotelBrowserPage = new HotelBrowserPageImpl();

    private CommentService commentService = new CommentServiceImpl();

    private BookingCRUDPage bookingCRUDPage = new BookingCRUDPageImpl();

    private BookingService bookingService = new BookingServiceImpl();

    private RoomService roomService = new RoomServiceImpl();

    private HistoryPage historyPage = new HistoryPageImpl();
    public void login(UserDTO userDTO) {
        try {
            if (BaseContext.getCurrentId() != null) {
                throw new MessageException(CommonConstant.ALREADY_LOGGED);
            }
            userService.login(userDTO);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void register(UserDTO userDTO) {
        try {
            if (BaseContext.getCurrentId() != null) {
                throw new MessageException(CommonConstant.ALREADY_LOGGED_NO_REGISTER);
            }
            userService.register(userDTO);
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void browse(Long hotelId) {
        while (true) {
            switch (hotelBrowserPage.show()) {
                case 1:
                    hotelService.showBasicInfo(hotelId);
                    break;
                case 2:
                    hotelService.showBasicAmenities(hotelId);
                    break;
                case 3:
                    hotelService.showComment(hotelId);
                    break;
                case 4:
                    commentService.getRating(hotelId);
                    break;
                case 5:
                    hotelService.showDescription(hotelId);
                    break;
                case 6:
                    return;
            }
        }
    }

    public void comment(Long hotelId) {
        try {
            if (BaseContext.getCurrentId()==null){
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            hotelService.comment(hotelId);
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void historicalOrders() {
        try {
            if (BaseContext.getCurrentId()==null){
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            userService.historicalOrders(historyPage.show());
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void checkInRoom() {
        try {
            if (BaseContext.getCurrentId()==null){
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            List<Room> list = roomService.listByStatus(1);
            if (list==null||list.size()==0){
                throw new MessageException(CommonConstant.NO_IDLE_ROOMS);
            }
            list.forEach(System.out::println);
            BookingDTO bookingDTO = bookingCRUDPage.add();
            bookingService.add(bookingDTO);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void checkOutRoom() {
        try {
            if (BaseContext.getCurrentId()==null){
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            userService.checkOutRoom();
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }
}
